﻿using System.Collections.Generic;
using UnityEditor;
using UnityEditor.AddressableAssets;

public class AutoAddLuaScriptsToAddressable : AssetPostprocessor
{
    private const string LUA_ROOT_FLODER = "LuaScripts";
    private const string TOLUA_FLODER = "ToLuaScripts";
    private const string LUA_FLODER = "CustomScripts";
    private const string PREFIX = "Assets/AddressableAssets/" + LUA_ROOT_FLODER;
    private const string POSTFIX = ".lua";
    private const string TEXT_POSTFIX = ".txt";
    private const string TOLUA_GROUP = "ToLuaScripts";
    private const string LUA_GROUP = "LuaScripts";
    private const string LUA_LABEL = "lua";

    static List<string> FilterAssets(string[] assets)
    {
        List<string> results = new List<string>();
        foreach (var asset in assets)
        {
            if (asset.StartsWith(PREFIX) && (asset.EndsWith(POSTFIX) || asset.EndsWith(TEXT_POSTFIX)))
            {
                if (asset.EndsWith(POSTFIX))
                    UnityEngine.Debug.LogWarning($"文件 {asset} 使用 .lua 后缀，无法从AssetBundle中加载");
                results.Add(asset);
            }
        }
        return results;
    }

    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        if (AddressableUtil.DefaultGroupName == null)
            return;

        var assetsToImport = FilterAssets(importedAssets);

        AddressableUtil.AddLabel(LUA_LABEL);

        if (assetsToImport.Count > 0)
        {
            foreach (var asset in assetsToImport)
            {
                string guid = null;
                if (!AddressableUtil.GetGUIDFromPath(asset, ref guid))
                    continue;
                getGroupAndAddress(asset, out var group, out var address);
                AddressableUtil.AddAssetToAddressableGroup(asset, address, group, LUA_LABEL,true);
            }
        }

        var assetsToDelete = FilterAssets(deletedAssets);
        if (assetsToDelete.Count > 0)
        {
            foreach (var asset in assetsToDelete)
            {
                AddressableUtil.RemoveAssetFromAddressable(asset);
            }
        }

        for (int i = 0; i < movedAssets.Length; i++)
        {
            var asset = movedAssets[i];
            var from = movedFromAssetPaths[i];
            if (from.StartsWith(PREFIX))
            {
                if (asset.StartsWith(PREFIX))
                {
                    AddressableUtil.RemoveAssetFromAddressable(asset);
                    getGroupAndAddress(asset, out var group, out var address);
                    AddressableUtil.AddAssetToAddressableGroup(asset, address, group, LUA_LABEL, true);
                }
                else
                    AddressableUtil.RemoveAssetFromAddressable(asset);
            }
            else if (asset.StartsWith(PREFIX))
            {
                getGroupAndAddress(asset, out var group, out var address);
                AddressableUtil.AddAssetToAddressableGroup(asset, address, group, LUA_LABEL, true);
            }
        }
    }

    private static void getGroupAndAddress(string assetPath, out string group, out string address)
    {
        if (assetPath.Contains(TOLUA_FLODER))
        {
            group = TOLUA_GROUP;
            address = assetPath.Substring(assetPath.IndexOf(TOLUA_FLODER)+ 1 + TOLUA_FLODER.Length);
            address = address.Substring(0, address.IndexOf('.')) + POSTFIX;
        }
        else
        {
            group = LUA_GROUP;
            address = assetPath.Substring(assetPath.IndexOf(LUA_FLODER) + 1 + LUA_FLODER.Length);
            address = address.Substring(0, address.IndexOf('.')) + POSTFIX;
        }
    }
}
